package com.goldcn.sys.model.util;

import com.github.pagehelper.util.StringUtil;
import com.goldcn.common.model.BaseModel;

/**
 * Created by Administrator on 2016/9/5.
 */
public class MiniuiPage extends BaseModel {
    private Integer pageIndex;
    private Integer pageSize;
    private String sortField;
    private String sortOrder;
    private String key;

    private String orderBy;
    public final static String ASC = "asc";
    public final static String DESC = "desc";

    public MiniuiPage(Integer pageIndex, Integer pageSize, String sortField, String sortOrder) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getOrderBy() {
        if(orderBy != null){
            return orderBy;
        }
        return sortField+" "+sortOrder;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public boolean isOrderDESC(){
        return MiniuiPage.DESC.equals(sortOrder);
    }


    /**
     *
     * @param pageIndex
     * @param pageSize
     * @param sortField
     * @param sortOrder
     * @param defaultOrderBy 默认排序如"updateTime desc"
     * @return
     */
    public static MiniuiPage getPageInfo(Integer pageIndex, Integer pageSize, String sortField, String sortOrder,String defaultOrderBy){
        MiniuiPage miniuiPage = new MiniuiPage(pageIndex,pageSize,sortField,sortOrder);
        if (StringUtil.isNotEmpty(miniuiPage.getSortField())){
            miniuiPage.setSortOrder(MiniuiPage.ASC);
            if (miniuiPage.isOrderDESC()) {
                miniuiPage.setSortOrder(MiniuiPage.DESC);
            }else
            {
                miniuiPage.setSortOrder(MiniuiPage.ASC);
            }
            miniuiPage.setOrderBy(miniuiPage.getOrderBy());
        }else{
            miniuiPage.setOrderBy(defaultOrderBy);
        }
        return miniuiPage;
    }

}
